package at.com.vn.chess.element;

import java.awt.Font;
import java.awt.Label;
import java.awt.List;
import java.awt.Point;
import java.awt.TextArea;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import vn.com.bip.office.smart.util.XMLHelper;
import vn.nnn.shogi.board.Board;
import vn.nnn.shogi.board.PrivateChatLayout;
import at.com.vn.chess.entity.ChatUser;
import at.com.vn.chess.listener.ChatListener;
import at.com.vn.chess.util.Const;

public class PrivateChatPanel extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	// Chat component
	public TextArea chatContent;
	public List mListUser;
	private Label lblChatTitle;
	private JTextField chatInputField;
	private ChatListener mChatListener;
	
	private ArrayList<ChatUser> lstReturnUser;
	private ChatUser chatUser;
	
	public PrivateChatPanel( Board board, ChatUser user ) {
		
		setSize( board.getPrivateChatAreaSize() );
		setLayout( null );
		
		chatUser = user;
		
		Font ver2 = new Font( "Verdana", 1, 12 );
		PrivateChatLayout layout = new PrivateChatLayout(
				new Point() );

		lblChatTitle = new Label();
		add( lblChatTitle );
		lblChatTitle.setBounds( layout.getTitleBound() );
		lblChatTitle.setFont( ver2 );
		lblChatTitle.setText( Const.PRIVATE_CHAT_TITLE );

		mListUser = new List( 100 );
		add( mListUser );
		mListUser.setBounds( layout.getListUserBound() );
		mListUser.setFont( ver2 );
		mListUser.add( " " );

		chatContent = new TextArea();
		add( chatContent );
		chatContent.setBounds( layout.getChatContentBound() );
		chatContent.setFont( ver2 );
		chatContent.setEditable( false );
		chatContent.setText( "Chess Table 1 on chat\n" );
		chatContent.append( "------------------------------------------\n" );

		chatInputField = new JTextField();
		add( chatInputField );
		chatInputField.setBounds( layout.getChatInputBound() );
		chatInputField.setFont( ver2 );
		chatInputField.setText( "" );

		SwingUtilities.invokeLater( new Runnable() {
			public void run() {
				chatInputField.requestFocus();
			}
		} );

		chatInputField.addKeyListener( new KeyAdapter() {
			public void keyPressed( KeyEvent e ) {
				int key = e.getKeyCode();
				if ( key == KeyEvent.VK_ENTER ) {
					Toolkit.getDefaultToolkit().beep();
					
					String xmlMsg = prepareChatMsg( Const.PROCESS_CHAT_PRIVATE_CHAT, chatInputField.getText() );
					
					mChatListener.chat( xmlMsg );
					chatInputField.setText( "" );
				}
			}
		} );
		
//		setVisible( true );
	}
	
	public void setOnChatListner( ChatListener chatListener ) {
		mChatListener = chatListener;
	}
	
	@SuppressWarnings( "unchecked" )
	public void showResult( String resultXML ) {

		String processName = XMLHelper.getProcessName( resultXML );
		if ( !processName.contains( Const.SERVICE_CHAT_PRIVATE ) ) {
			return;
		}

		lstReturnUser = ( ArrayList<ChatUser> ) XMLHelper
				.convertXMLToListObject( resultXML, ChatUser.class );
		
		if( lstReturnUser.get( 0 ).getRoomID().equals( chatUser.getRoomID() )
				&& lstReturnUser.get( 0 ).getPrivateRoomID().equals( chatUser.getPrivateRoomID() ) ) {
			
			showResult( lstReturnUser, processName );	
		}		
	}
	
	private void showResult( ArrayList<ChatUser> listUser, String processName ) {

		if ( !isActiveUserOnChessTable( listUser ) ) {
			return;
		}

		// Refresh listUser if not chat
		if ( !processName.contains( "_chat" ) ) {
			mListUser.removeAll();
			for ( ChatUser user : listUser ) {
				if ( isOnChessTable( user ) ) {
					if ( user.getIsDeleted() != Const.TRUE ) {
						mListUser.add( user.getUserID() );
					}
				}
			}
		}

		// Show message: join, left, chat
		for ( ChatUser user : listUser ) {
			if ( isOnChessTable( user ) ) {
				if ( user.getIsActive().equals( 1 ) ) {
					if ( !processName.contains( "_chat" ) ) {
						chatContent.append( user.getUserID() + " "
								+ user.getChatMsg() + "\n" );
					} else {
						chatContent.append( user.getUserID() + " : "
								+ user.getChatMsg() + "\n" );
					}
				}
			}
		}
	}

	private boolean isOnChessTable( ChatUser user ) {
		if ( user.getRoomID().equals( user.getRoomID() )
				&& user.getPrivateRoomID().equals( user.getPrivateRoomID() ) ) {
			return true;
		}
		return false;
	}

	private boolean isActiveUserOnChessTable( ArrayList<ChatUser> lstUser ) {
		for ( ChatUser user : lstReturnUser ) {
			if ( isOnChessTable( user ) ) {
				return true;
			}
		}
		return false;
	}

	public String prepareChatMsg( String propName, String message ) {
		ChatUser user = new ChatUser();

		user.setUserID( chatUser.getUserID() );
		user.setRoomID( chatUser.getRoomID() );
		user.setChatMsg( message );
		user.setIsActive( Const.TRUE );
		user.setPrivateRoomID( chatUser.getPrivateRoomID() );
		user.setIsDeleted( Const.FALSE );

		return XMLHelper.convertListObjectToXML( user, propName, ChatUser.class );
	}

}
